package com.pgytesting.secondhandcar.controller.mcpuser;

import com.pgytesting.secondhandcar.model.CreditInformation;
import com.pgytesting.secondhandcar.model.RepaymentBill;
import com.pgytesting.secondhandcar.model.SupplementaryBasicInformation;
import com.pgytesting.secondhandcar.model.VehicleInformation;
import com.pgytesting.secondhandcar.service.*;
import com.pgytesting.secondhandcar.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author : frosty0804@gmail.com
 * @version : v1.0
 * @projectName : secondhandcar
 * @className : UseApplyForLoanController
 * @description : TODO(贷款)
 * @date : 2019/8/21 17:08
 */
@Slf4j
@RestController
@RequestMapping("/user/loan")
public class UseApplyForLoanController {


    @Autowired
    OrderNameService orderNameService;

    @Autowired
    SupplementaryBasicInformationService basicInformationService;

    @Autowired
    CreditInformationService creditInformationService;

    @Autowired
    LoanRepaymentInformationService loanRepaymentInformationService;

    @Autowired
    RepaymentBillService repaymentBillService;


    /**
     * 初步申请贷款
     *
     * @param vehicleInformation 参数
     * @param bindingResult      参数验证
     * @return
     */
    @RequestMapping(value = "/preliminaryApplication", method = RequestMethod.POST)
    public ResponseMessage applyLoan(@RequestBody @Valid VehicleInformation vehicleInformation, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String msg = bindingResult.getFieldError().getDefaultMessage();
            return ResponseMessage.errorResponse(msg);
        }
        try {
            return orderNameService.insert(vehicleInformation);
        } catch (Exception e) {
            log.error("服务异常", e);
            return ResponseMessage.errorResponse("服务异常");
        }
    }


    /**
     * 继续完善贷款信息
     *
     * @param basicInformation 参数
     * @return
     */
    @RequestMapping(value = "/improveInformation", method = RequestMethod.POST)
    public ResponseMessage improveInformation(@RequestBody SupplementaryBasicInformation basicInformation) {
        try {
            return basicInformationService.insert(basicInformation);
        } catch (Exception e) {
            log.error("服务异常", e);
            return ResponseMessage.errorResponse("服务异常");
        }
    }


    /**
     * 上传征信授权
     *
     * @param creditInformation 参数
     * @param bindingResult     参数验证
     * @return
     */
    @RequestMapping(value = "/uploadCredit", method = RequestMethod.POST)
    public ResponseMessage uploadCredit(@Valid @RequestBody CreditInformation creditInformation, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String msg = bindingResult.getFieldError().getDefaultMessage();
            return ResponseMessage.errorResponse(msg);
        }
        try {
            return creditInformationService.insert(creditInformation);
        } catch (Exception e) {
            log.error("服务异常", e);
            return ResponseMessage.errorResponse("服务异常");
        }
    }


    /**
     * 查询所有已还款的数据
     *
     * @return
     */
    @RequestMapping("/queryLoan")
    public ResponseMessage queryLoan() {
        try {
            return repaymentBillService.queryAllRepaid();
        } catch (Exception e) {
            log.error("服务异常", e);
            return ResponseMessage.errorResponse("服务异常");
        }
    }


    /**
     * 查看还款信息详情 状态码：0表示未还款1表示已还款2表示已到还款日等待用户还款3是已逾期
     *
     * @return
     */
    @RequestMapping(value = "/checkRepayment", method = RequestMethod.POST)
    public ResponseMessage checkRepayment() {
        try {
            return repaymentBillService.queryAll();
        } catch (Exception e) {
            log.error("服务异常", e);
            return ResponseMessage.errorResponse("服务异常");
        }
    }

    /**
     * 还款
     *
     * @param repaymentBill 还款
     * @return
     */
    @RequestMapping(value = "/repayment", method = RequestMethod.POST)
    public ResponseMessage repayment(@RequestBody RepaymentBill repaymentBill) {
        return repaymentBillService.update(repaymentBill);
    }

}
